﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors.Repository;
using DevExpress.LookAndFeel;
using DevExpress.XtraGrid.Columns;
using CIRDiagnoseDevice.Test;
using System.IO.Ports;
using System.Threading;
using System.Runtime.InteropServices;
using System.Reflection;
using CIRDiagnoseDevice.DBManage;


namespace CIRDiagnoseDevice
{
    public partial class WholeDeviceTestFrm : Form
    {

        #  region  全局变量
        private bool m_checkStatus = false;
        private List<string[,]> list450M = new List<string[,]>();
        private List<string[,]> listGSM = new List<string[,]>();
        private List<string[,]> listFun = new List<string[,]>();
        private List<string[,]> list800M = new List<string[,]>();
        public string TestTime, Tester, Dev_num; // 保存测试时间、测试员、CIR编号
        private _450MFunTrial TrainEndFunTrial;
        private CIR_450M_CoRecModTest CoRecTest;
        private CIR_450M_PiRecModTest PiRecTest;
        private CIR_450M_SigModTest SigTest;
        private CIR_450M_TranModTest TranTest;
        private GSM_RDigModTest GsmDigTest;
        private GSM_RFunTrial GsmFunTrial;
        private GSM_RVoModTest GsmVoTest;
        private LBJFunTrial LbjFunTrial;
        private LbjRecTest LbjRecTest;
        private LbjTranTest LbjTranTest;
        private TrainApproachEarlyWarning TrainApproach;
        private CheckBox HeaderCheckBox;
        private CheckBox HeaderCheckBox2;
        private Rectangle oRectangle;
        private GSM_Test Gsm;
        private InitSerialPort InitPort = new InitSerialPort();
        private string selectedPageName;
        private _450MDispatchCommTrial _450MDispatchTrial;
        private int ResetTimes;
        private int StartNum;
        private TestRecordManage trm = new TestRecordManage();
        string[,] itemName = { { "发射机性能", "载波频率容差","--", "≤5×10ˉ6" }, { "发射机性能", "载波功率", "W" ,"5、10、( -15%~+20%)"},{"发射机性能", "调制特性(300Hz~3000Hz)","dB","-3~+1"},
                                               {"发射机性能", "调制限制",  "KHz","≤ 5"},{"发射机性能", "高调制时的发射机频偏(5 KHz)"," Hz ","≤ 1500"}, {"发射机性能", "高调制时的发射机频偏(3 KHz~5 KHz)","--","频偏单调下降"},
                                               {"发射机性能", "调制灵敏度", "mV","245"},{"发射机性能", "音频失真", "% "," ≤ 5"},{"发射机性能", "剩余调频", "dB","≤ -40"}};


        string[,] itemName2 = { { "同频接收机性能","参考灵敏度(单工)",  "μV"," ≤ 0.6"},{"同频接收机性能","调制接收宽度", "KHz"," ≥2×5"},{"同频接收机性能","接收门限",  "μV"," 0.6~5"},{"同频接收机性能","额定音频输出功率 (扬声器)"," W","0.5~5 可调"},
                                             { "同频接收机性能","音频响应(扬声器)","dB","-8~2"},{"同频接收机性能","额定音频输出功率 (耳机)", "mW"," 1~10可调"}, {"同频接收机性能","音频失真",  "%"," ≤5"},{"同频接收机性能","音频响应(耳机)","dB"," -3 ~1"},
                                             { "同频接收机性能","信号对剩余输出功率比", "dB"," ≤-40"}};


        string[,] itemName3 = { {"异频接收机性能","参考灵敏度(单工)",  "μV"," ≤ 0.6"},{"异频接收机性能","双工灵敏度","μ V","不低于单工3dB"},{"异频接收机性能","调制接收宽度", "KHz"," ≥2×5"},{"异频接收机性能","接收门限",  "μ V"," 0.6~5"},{"异频接收机性能","额定音频输出功率 (扬声器)"," W","0.5~5 可调"},
                                             { "异频接收机性能","音频响应(扬声器)","dB","-8~2"}, { "异频接收机性能","额定音频输出功率 (耳机)", "mW"," 1~10可调"}, { "异频接收机性能","音频失真",  "%"," ≤5"},{ "异频接收机性能","音频响应(耳机)","dB"," -3 ~1"}, 
                                             { "异频接收机性能","信号对剩余输出功率比", "dB"," ≤-40"}}; //,{"异频接收机性能","双工灵敏度","μ V","不低于单工3dB"}


        string[,] itemName4 = { {"信令信号频偏","1520Hz",  "KHz","3(容差+15%)"},{"信令信号频偏","114.8Hz",  "KHz","0.5(容差+15%)"},{"信令信号频偏", "186.2Hz", "KHz","0.5(容差+15%)"},
                                      {"信令信号频偏","123Hz",  "KHz","0.5(容差+15%)"},{"信令信号频偏", "131.8Hz", "KHz","0.5(容差+15%)"},{"信令信号频偏","151.4Hz",  "KHz","0.5(容差+15%)"},
                                      {"信令信号频偏", "162.2Hz", "KHz","0.5 (容差+15%)"},{"信令信号频偏", "173.8Hz", "KHz","0.5 (容差+15%)"},{"信令信号频率准确度","1520Hz", "%","0.5"},
                                      {"信令信号频率准确度", "114.8Hz", "%","0.5"}, {"信令信号频率准确度", "186.2Hz", "%","0.5"}, {"信令信号频率准确度", "123Hz", "%","0.5"}, {"信令信号频率准确度", "131.8Hz", "%","0.5"},
                                      {"信令信号频率准确度", "151.4Hz", "%","0.5"}, {"信令信号频率准确度", "162.2Hz", "%","0.5"}, {"信令信号频率准确度", "173.8Hz", "%","0.5"},
                                      {"信令信号检出特性","114.8Hz(同频)","--","在6dB信纳比，频偏 0.5 KHz时,解码电路工作"}, {"信令信号检出特性","1960Hz","--","在6dB信纳比，频偏 1.75KHz时，解码电路工作"},
                                    {"信令信号检出特性","114.8Hz(异频)","--","在6dB信纳比，频偏 0.5 KHz时,解码电路工作"},{"信令信号检出特性","114.8Hz+186.2Hz)","--","在6dB信纳比，频偏 0.5 KHz时,解码电路工作"} ,
                                    {"信令信号接收带宽","1960Hz", "%","低端： -2.5 ~ -1.5，高端：1.5 ~ 2.5"} , {"信令信号接收带宽","114.8Hz(同频)", "%","低端：-2.0 ~ -1.0，高端：1.0 ~ 2.0"}, 
                                    {"信令信号接收带宽","114.8Hz(异频)", "%","低端： -2.0 ~ -1.0 ，高端：1.0 ~ 2.0"} }; //  {"信令信号接收带宽","186.2Hz(异频)", "%","低端： -2.0 ~ -1.0，高端：1.0 ~ 2.0"},

        string[,] itemName5 = { {"GSM-R语音模块标识","IMEI","---"},{"GSM-R语音模块发射指标","最大峰值功率","39dBm±2dB"},{"GSM-R语音模块发射指标","载波频率误差","≤89Hz"},{"GSM-R语音模块发射指标","峰值相位误差","≤20°"},
                                               {"GSM-R语音模块发射指标","均方根相位误差","≤5°"},{"GSM-R语音模块发射指标","发射机突发脉冲定时","正常测试条件下,各功率等级下的正常突发脉冲功率时间包络应落在规定的包络曲线内"},
                                               {"GSM-R语音模块接收指标","接收灵敏度","优于-102dBm"}};


        string[,] itemName6 = {{"GSM-R数据模块标识","IMEI","---"}, {"GSM-R数据模块发射指标","最大峰值功率","33dBm±2dB"},{"GSM-R数据模块发射指标","载波频率误差","≤89Hz"},{"GSM-R数据模块发射指标","峰值相位误差","≤20°"},
                                               {"GSM-R数据模块发射指标","均方根相位误差","≤5°"},{"GSM-R数据模块发射指标","发射机突发脉冲定时","正常测试条件下,各功率等级下的正常突发脉冲功率时间包络应落在规定的包络曲线内"},
                                               {"GSM-R数据模块接收指标","接收灵敏度","优于-102dBm"}};

        string[,] itemName7 = { {"866信道发射机","载波频率容差","--", "≤5×10ˉ6" }, {"866信道发射机","载波功率"," W" ,"3~10 可调"},{"866信道发射机","调制频偏(1.2 KHz)","KHz","3.0~3.5"},{"866信道发射机","调制信号频率准确度(1.2 KHz)", "%","0.5"}, 
                              {"866信道发射机","调制限制", "KHz","≤ 5"},{"866信道发射机","剩余调频", "dB","≤ -40"},{"821信道发射机","载波频率容差","--", "≤5×10ˉ6" }, {"821信道发射机","载波功率"," W" ,"3~10 可调"},{"821信道发射机","调制频偏(1.2 KHz)","KHz","4.2~4.8"}, 
                              { "866信道接收机", "参考灵敏度", "μ V", " ≤ 0.6" }, { "866信道接收机", "调制接收宽度", "KHz", " ≥2×5" }, {"866信道接收机", "音频失真",  "%"," ≤5"}, {"866信道接收机", "信号对剩余输出功率比", "dB"," ≤-40"} };


        string[,] item = new string[,]{{ "LBJ功能试验","KLW连接测试"},{ "LBJ功能试验","KLW手动查询风压测试"},{ "LBJ功能试验","KLW辅助排风制动测试"},{"LBJ功能试验","KLW风压报警测试"},{ "LBJ功能试验","KLW电压报警测试"},
                                                   {  "LBJ功能试验",  "KLW取消连接测试"},{ "LBJ功能试验","列车防护报警发送信息测试"},{ "LBJ功能试验","列车防护报警接收信息测试"},{ "LBJ功能试验","道口事故报警信息测试"},{ "LBJ功能试验","施工防护报警信息测试"}};//,{"LBJ功能试验", "列车接近预警信息测试"}};


        string[,] item2 = new string[,] { { "450MHz列尾功能试验", "列尾连接确认" }, { "450MHz列尾功能试验", "风压查询" }, { "450MHz列尾功能试验", "排风制动" }, { "450MHz列尾功能试验", "风压报警" }, { "450MHz列尾功能试验", "欠压报警" }, { "450MHz列尾功能试验", "列尾消号" }, { "450M调度命令功能试验", "调度命令接收" } };

        string[,] item3 = new string[,] { { "GSM-R功能试验", "无线车次号上传" }, { "GSM-R功能试验", "调度命令接收" }, { "GSM-R功能试验", "进路预告命令接收" } };
        #  endregion

        #   region 设置CheckBox不可选/可选
        public void SetCheckBoxUnEdit(bool unEdit)
        {
            if (dataGridView1.InvokeRequired)
            {
                dataGridView1.Invoke(new MethodInvoker(delegate
                {
                    dataGridView1.Columns[0].ReadOnly = unEdit;
                }));
            }
            else
            {
                dataGridView1.Columns[0].ReadOnly = unEdit;
            }
        }
        #   endregion

        [DllImport("user32.dll", EntryPoint = "FindWindow", CharSet = CharSet.Auto)]
        private extern static IntPtr FindWindow(string lpClassName, string lpWindowName); //用于找到MessageBox的句柄
        [DllImport("user32.dll")]
        static extern bool EndDialog(IntPtr hDlg, out IntPtr nResult);

        //根据句柄关闭MessageBox

        # region 关闭MessageBox窗体
        private void CloseMessageBox(string formText)
        {
            IntPtr ptr = FindWindow(null, formText);//查找MessageBox的弹出窗口,注意对应标题
            if (ptr != IntPtr.Zero)
            {
                IntPtr result;
                EndDialog(ptr, out result); //查找到窗口则关闭
            }

        }

        #endregion

        public void Test450MQuota(ref Thread tranThead, ref Thread coThread, ref Thread piThread, ref Thread sigThread)
        {
            // if (TranTest == null)
            {
                TranTest = new CIR_450M_TranModTest(this, 0, null, MainFrm.MotherFrm._2944SerialPort, MainFrm.MotherFrm.queue);
            }
            tranThead = new Thread(new ParameterizedThreadStart(TranTest.TestTrismitter));
            tranThead.IsBackground = true;
            TranTest.InitParameter();

            //if (CoRecTest == null)
            {
                CoRecTest = new CIR_450M_CoRecModTest(this, 9, tranThead, dataGridView1, MainFrm.MotherFrm._2944SerialPort, MainFrm.MotherFrm.queue);
            }
            coThread = new Thread(new ThreadStart(CoRecTest.TestCo_Reciever));
            coThread.IsBackground = true;
            CoRecTest.InitParameter();

            // if (PiRecTest == null)
            {
                PiRecTest = new CIR_450M_PiRecModTest(this, 18, coThread, MainFrm.MotherFrm._2944SerialPort, MainFrm.MotherFrm.queue);
            }
            piThread = new Thread(new ParameterizedThreadStart(PiRecTest.TestPilot_Reciever));
            piThread.IsBackground = true;
            PiRecTest.InitParameter();

            //if (SigTest == null)
            {
                SigTest = new CIR_450M_SigModTest(this, 28, piThread, MainFrm.MotherFrm._2944SerialPort, MainFrm.MotherFrm.queue);
            }
            SigTest.OnMessageNotice += new MessageNotice(SigTest_OnMessageNotice);
            sigThread = new Thread(new ParameterizedThreadStart(SigTest.TestSigStandard));
            sigThread.IsBackground = true;
            SigTest.InitParameter();
            SetCheckBoxUnEdit(true);
            try
            {
                InitGrid(dataGridView1);
                tranThead.Start(dataGridView1);
                coThread.Start();
                piThread.Start(dataGridView1);
                sigThread.Start(dataGridView1);
                MainFrm.MotherFrm.TestTotalSaved = false;
            }
            catch (ThreadInterruptedException e)
            {

            }
        }

        public string SelectedPageName
        {
            get { return selectedPageName; }
        }

        void SigTest_OnMessageNotice(object sender, ThreadRunState e)
        {
            switch (e)
            {
                case ThreadRunState.End:
                    this.Invoke(new MethodInvoker(delegate
                    {
                        xtraTabControl1.SelectedTabPageIndex = 1;
                        this.Invalidate();
                    }));
                    MainFrm.MotherFrm.StopTest = true;
                    MainFrm.MotherFrm.Suspend = true;
                    MainFrm.MotherFrm.CloseSerialPort(MainFrm.MotherFrm._2944SerialPort);
                    MainFrm.MotherFrm.StopTest = false;
                    MainFrm.MotherFrm.Suspend = false;
                    MainFrm.MotherFrm.TestGsmQuotaMethod();
                    break;
                default:
                    break;
            }
        }

        public void TestLbjQuota(ref Thread lbjTranThread, ref Thread lbjRecThread)
        {
            if (MessageBox.Show("现在测试LBJ电性能指标，请将2945B仪表与LBJ连接，\r\n并将接口单元的LBJ相关接口连接到LBJ的维护口上", "提示", MessageBoxButtons.OK) == DialogResult.OK)
            {
                Clear450MSource();
                LbjTranTest = new LbjTranTest(MainFrm.MotherFrm._2944SerialPort, 0, null, MainFrm.MotherFrm.queue, MainFrm.MotherFrm.LbjNorSerialPort);
                lbjTranThread = new Thread(new ParameterizedThreadStart(LbjTranTest.TestLbjTransmitter));
                lbjTranThread.IsBackground = true;
                LbjRecTest = new LbjRecTest(MainFrm.MotherFrm._2944SerialPort, MainFrm.MotherFrm.LbjNorSerialPort, 9, lbjTranThread, MainFrm.MotherFrm.queue);
                lbjRecThread = new Thread(new ParameterizedThreadStart(LbjRecTest.LbjRecNorTest));
                lbjRecThread.IsBackground = true;
                LbjRecTest.OnMessageNote += new MessageNotice(LbjRecTest_OnMessageNote);
                MainFrm.MotherFrm.TestLbjTran = true;
                MainFrm.MotherFrm.TestLbjRec = true;
                MainFrm.MotherFrm.StopTest = false;
                MainFrm.MotherFrm.Suspend = false;
                InitGrid(dataGridView3);
                lbjTranThread.Start(dataGridView3);
                lbjRecThread.Start(dataGridView3);
            }
        }

        void LbjRecTest_OnMessageNote(object sender, ThreadRunState e)
        {
            switch (e)
            {
                case ThreadRunState.End:
                    this.Invoke(new MethodInvoker(delegate
                    {
                        xtraTabControl1.SelectedTabPageIndex = 3;
                        this.Invalidate();
                    }));
                    MainFrm.MotherFrm.StopTest = true;
                    MainFrm.MotherFrm.Suspend = true;
                    MainFrm.MotherFrm.CloseSerialPort(MainFrm.MotherFrm._2944SerialPort);
                    MainFrm.MotherFrm.StopTest = false;
                    MainFrm.MotherFrm.Suspend = false;
                    MainFrm.MotherFrm.TrialLbjFun();
                    break;
                default:
                    break;
            }
        }

        public void TestGsmQuota(ref Thread gsmVoiThread, ref Thread gsmDigThread)
        {
            if (MessageBox.Show("现在测试GSM-R模块指标，请将线路切换到GSM-R模式\r\n并将4202测试仪表与CIR的GSM-R语音模块连接", "提示", MessageBoxButtons.OK) == DialogResult.OK)
            {
                // if (GsmVoTest == null)
                {
                    GsmVoTest = new GSM_RVoModTest(this, dataGridView2, 0, MainFrm.MotherFrm._4202SerialPort, MainFrm.MotherFrm.queue, MainFrm.MotherFrm.GSM_RModPort);
                }
                //GsmVoTest.StartTestGSM_RVo();
                gsmVoiThread = new Thread(new ThreadStart(GsmVoTest.Test_Gsm_rVoMod));
                gsmVoiThread.IsBackground = true;
                //  if (GsmDigTest == null)
                {
                    GsmDigTest = new GSM_RDigModTest(this, dataGridView2, 7, MainFrm.MotherFrm._4202SerialPort, MainFrm.MotherFrm.queue, gsmVoiThread);
                }
                GsmDigTest.OnMessageNotice += new MessageNotice(GsmDigTest_OnMessageNotice);
                gsmDigThread = new Thread(new ThreadStart(GsmDigTest.TestGSM_RDigMod));
                gsmDigThread.IsBackground = true;
                InitPort.Open(MainFrm.MotherFrm._4202SerialPort);
                MainFrm.MotherFrm._4202SerialPort.DataReceived += new SerialDataReceivedEventHandler(_4202SerialPort_DataReceived);
                MainFrm.MotherFrm._4202SerialPort.ReadTimeout = 55000;
                GsmDigTest.InitParameter();
                GsmVoTest.InitParameter();
                InitGrid(dataGridView2);
                gsmVoiThread.Start();
                gsmDigThread.Start();
                Gsm = new GSM_Test();
            }
        }

        void _4202SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            try
            {
                while (!MainFrm.MotherFrm.StopTest)
                {
                    try
                    {
                        byte readByte = (byte)MainFrm.MotherFrm._4202SerialPort.ReadByte();
                        // lock (MainFrm.MotherFrm.queue)
                        {
                            MainFrm.MotherFrm.queue.Enqueue(readByte);
                        }

                    }
                    catch (Exception e1)
                    {
                        if (!MainFrm.MotherFrm.StopTest)
                        {
                            if (e1.GetType() == typeof(TimeoutException))
                            {
                                if (MainFrm.MotherFrm.TestGSM_RVo)
                                {
                                    if (ResetTimes == 0)
                                    {
                                        ResetTimes++;
                                        CloseMessageBox("用户操作提示");
                                        GsmVoTest.StartTestGSM_RVo();
                                    }
                                    else
                                    {
                                        ResetTimes = 0;
                                        if (MessageBox.Show("是否没有将CIR语音模块连接到4202仪表？", "用户操作提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
                                        {
                                            CloseMessageBox("用户操作提示");
                                            GsmVoTest.StartTestGSM_RVo();
                                        }
                                        else
                                        {
                                            GsmVoTest.DealWithUnqualified();
                                        }
                                    }
                                }
                                else if (MainFrm.MotherFrm.TestGSM_RDig)
                                {
                                    if (GsmDigTest.haveReadIMEI)
                                    {
                                        GsmDigTest.TestGSM_DigMod();
                                    }
                                    else
                                    {
                                        GsmDigTest.DealWithUnqualified();
                                    }
                                }
                            }
                        }
                    }
                }
                return;
            }
            catch
            { }
        }

        void GsmDigTest_OnMessageNotice(object sender, ThreadRunState e)
        {
            switch (e)
            {
                case ThreadRunState.End:
                    this.Invoke(new MethodInvoker(delegate
                  {
                      xtraTabControl1.SelectedTabPageIndex = 2;
                  }));
                    MainFrm.MotherFrm.TestTotalSaved = false;
                    this.Invalidate();
                    MainFrm.MotherFrm.StopTest = true;
                    MainFrm.MotherFrm.CloseSerialPort(MainFrm.MotherFrm._4202SerialPort);
                    MainFrm.MotherFrm.StopTest = false;
                    MainFrm.MotherFrm.TestLbjQuotaMethod();
                    break;
                default:
                    break;
            }
        }

        public void TrialFunction(ref Thread lbjThread)
        {
            if (GsmFunTrial == null)
            {
                GsmFunTrial = new GSM_RFunTrial(this, dataGridView4, 17, MainFrm.MotherFrm.GsmFunSerialPort, MainFrm.MotherFrm.TaxSerialPort);
                GsmFunTrial.OnMessageNotice += new MessageNotice(GsmFunTrial_OnMessageNotice);
            }
            if (_450MDispatchTrial == null)
            {
                _450MDispatchTrial = new _450MDispatchCommTrial(dataGridView4, 16, GsmFunTrial);
                _450MDispatchTrial.OnMessageNotice += new MessageNotice(_450MDispatchTrial_OnMessageNotice);
            }
            if (LbjFunTrial == null)
            {
                LbjFunTrial = new LBJFunTrial(this, dataGridView4, 0, MainFrm.MotherFrm.LbjSerialPort);
                LbjFunTrial.OnMessageNote += new ThreadEndNotice(LbjFunTrial_OnMessageNotice);
            }
            InitGrid(dataGridView4);
            LbjFunTrial.InitParameter();
            lbjThread = new Thread(new ThreadStart(LbjFunTrial.DealWithData));
            lbjThread.IsBackground = true;
            lbjThread.Start();
            // if (TrainEndFunTrial == null)
            {
                TrainEndFunTrial = new _450MFunTrial(this, dataGridView4, 10, MainFrm.MotherFrm.TrainEndSerialPort, lbjThread);
                TrainEndFunTrial.OnMessageNotice += new MessageNotice(TrainEndFunTrial_OnMessageNotice);
            }
            MainFrm.MotherFrm.Start450MFunTrial();
        }

        void _450MDispatchTrial_OnMessageNotice(object sender, ThreadRunState e)
        {
            GsmFunTrial.StartGsmFunTrial();
            MainFrm.MotherFrm.GSMFunTrialThread = new Thread(new ThreadStart(GsmFunTrial.DealWithData));
            MainFrm.MotherFrm.GSMFunTrialThread.IsBackground = true;
            MainFrm.MotherFrm.GSMFunTrialThread.Start();
        }


        public void TrialTrainApproach(SerialPort OSP, ref Thread trainAppThread)
        {
            if (TrainApproach == null)
            {
                TrainApproach = new TrainApproachEarlyWarning(OSP, dataGridView4, 10);
            }
        }

        public void Trial450MDispath(SerialPort OSP, ref Thread dispathThread)
        {
            _450MDispatchTrial.InitParameter(OSP);
            _450MDispatchTrial.Start450MDispatch(ref dispathThread);
        }

        void LbjFunTrial_OnMessageNotice(object sender, ThreadState e)
        {
            //switch (e)
            //{
            //    case ThreadState.End:

            //        break;
            //    default:
            //        break;
            //}
        }

        public void Trial450MFun(ref Thread trainEndThread)
        {
            if (trainEndThread != null)
            {
                trainEndThread.Abort();
            }
            try
            {
                TrainEndFunTrial.InitParameter();
                trainEndThread = new Thread(new ThreadStart(TrainEndFunTrial.DealWithReceiveData));
                trainEndThread.IsBackground = true;
                trainEndThread.Start();
            }
            catch
            {

            }
        }


        void TrainEndFunTrial_OnMessageNotice(object sender, ThreadRunState e)
        {
            MainFrm.MotherFrm._450MDispatchFunTrial = true;
            MainFrm.MotherFrm.Suspend = false;
            MainFrm.MotherFrm.StopTest = false;
            MainFrm.MotherFrm.Trial450MDispatch();
        }

        void GsmFunTrial_OnMessageNotice(object sender, ThreadRunState e)
        {
            MainFrm.MotherFrm.TestTotalSaved = false;
        }

        public void ClearDataGrid()
        {
            if (TranTest != null)
            {
                InitGrid(dataGridView1);
            }
            if (GsmVoTest != null)
            {
                InitGrid(dataGridView2);
            }
            if (LbjFunTrial != null)
            {
                InitGrid(dataGridView4);
            }
            if (LbjTranTest != null)
            {
                InitGrid(dataGridView3);
            }
        }

        public void InitGrid(DataGridView dgv)
        {
            if (dgv.InvokeRequired)
            {
                dgv.Invoke(new MethodInvoker(delegate
                {
                    if (dgv.Name == "dataGridView4")
                    {
                        for (int i = 0; i < dgv.Rows.Count; i++)
                        {
                            dgv.Rows[i].Cells["试验结果"].Value = "";
                        }
                    }
                    else
                    {
                        for (int i = 0; i < dgv.Rows.Count; i++)
                        {
                            dgv.Rows[i].Cells["测试值"].Value = "";
                            dgv.Rows[i].Cells["测试结果"].Value = "";
                        }
                    }
                }));
            }
            else
            {
                if (dgv.Name == "dataGridView4")
                {
                    for (int i = 0; i < dgv.Rows.Count; i++)
                    {
                        dgv.Rows[i].Cells["试验结果"].Value = "";
                    }
                }
                else
                {
                    for (int i = 0; i < dgv.Rows.Count; i++)
                    {
                        dgv.Rows[i].Cells["测试值"].Value = "";
                        dgv.Rows[i].Cells["测试结果"].Value = "";
                    }
                }
            }
        }

        public WholeDeviceTestFrm(ref Thread gsmDigThread, ref Thread gsmVoiThread)
        {
            try
            {
                this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw | ControlStyles.AllPaintingInWmPaint, true);
                this.UpdateStyles();
                InitializeComponent();
                list450M.Add(itemName);
                list450M.Add(itemName2);
                list450M.Add(itemName3);
                list450M.Add(itemName4);
                listGSM.Add(itemName5);
                listGSM.Add(itemName6);
                list800M.Add(itemName7);
                listFun.Add(item);
                listFun.Add(item2);
                listFun.Add(item3);
                selectedPageName = xtraTabControl1.TabPages[0].Text;
                Gsm = new GSM_Test();
                Type dgvType2 = this.dataGridView1.GetType();
                PropertyInfo pi2 = dgvType2.GetProperty("DoubleBuffered", BindingFlags.Instance | BindingFlags.NonPublic);
                pi2.SetValue(this.dataGridView1, true, null);

                Type dgvType = this.dataGridView2.GetType();
                PropertyInfo pi = dgvType.GetProperty("DoubleBuffered", BindingFlags.Instance | BindingFlags.NonPublic);
                pi.SetValue(this.dataGridView2, true, null);
            }
            catch
            {

            }
        }

        private DataTable InitGrid(byte displaytype)
        {
            //DataTable dt = new DataTable();
            //if (listItem[0].GetLength(1) == 4)
            //{
            //    dt.Columns.AddRange(new DataColumn[] {new DataColumn("全选", typeof(Boolean)),
            //                                                                  new DataColumn("分类", typeof (string)),
            //                                                                  new DataColumn("测试项目", typeof (string)),
            //                                                                  new DataColumn("单位",        typeof (string)),
            //                                                                  new DataColumn("指标要求",  typeof (string)),
            //                                                                  new DataColumn("测试值",      typeof(string)),
            //                                                                  new DataColumn("测试结果",   typeof(string))});
            //    for (int i = 0; i < listItem.Count; i++)
            //    {
            //        for (int j = 0; j < listItem[i].GetLength(0); j++)
            //        {
            //            DataRow row = dt.NewRow();
            //            row[0] = true;
            //            row[1] = listItem[i][j, 0];
            //            row[2] = listItem[i][j, 1];
            //            row[3] = listItem[i][j, 2];
            //            row[4] = listItem[i][j, 3];
            //            row[5] = "";
            //            row[6] = "";
            //            dt.Rows.Add(row);
            //        }
            //    }
            //}
            //else if (listItem[0].GetLength(1) == 3)
            //{
            //    dt.Columns.AddRange(new DataColumn[] {  new DataColumn("分类", typeof(string)),
            //                                                                        new DataColumn("测试项目", typeof(string)),
            //                                                                        new DataColumn("指标要求", typeof(string)),
            //                                                                        new DataColumn("测试值",    typeof(string)),
            //                                                                        new DataColumn("测试结果", typeof(string))});
            //    for (int i = 0; i < listItem.Count; i++)
            //    {
            //        for (int j = 0; j < listItem[i].GetLength(0); j++)
            //        {
            //            DataRow row = dt.NewRow();
            //            row[0] = listItem[i][j, 0];
            //            row[1] = listItem[i][j, 1];
            //            row[2] = listItem[i][j, 2];
            //            row[3] = "";
            //            row[4] = "";
            //            dt.Rows.Add(row);
            //        }
            //    }
            //}
            //else
            //{
            //    dt.Columns.AddRange(new DataColumn[] { new DataColumn("分类", typeof (string)),
            //                                                                       new DataColumn("试验项目",  typeof (string)),
            //                                                                       new DataColumn("试验结果",  typeof (string))});
            //    for (int i = 0; i < listItem.Count; i++)
            //    {
            //        for (int j = 0; j < listItem[i].GetLength(0); j++)
            //        {
            //            DataRow row = dt.NewRow();
            //            row[0] = listItem[i][j, 0];
            //            row[1] = listItem[i][j, 1];
            //            row[2] = "";
            //            dt.Rows.Add(row);
            //        }
            //    }
            //}
            trm.OpenDataBase();
            DataTable dt = new DataTable();

            switch (displaytype)
            {
                case 1:
                case 3:
                    {
                        dt.Columns.AddRange(new DataColumn[] { new DataColumn("全选", typeof(Boolean)) });
                        dt.Columns[0].DefaultValue = true;
                        StringBuilder sb = new StringBuilder();
                        sb.Append("select distinct TestGroup as N'分类',SUBSTRING(TestName, 1, case  when charindex('#',TestName )=0 then len(TestName) ");
                        sb.Append(" else charindex('#',TestName )-1 end ) AS N'测试项目',unit AS N'单位',remark AS N'指标要求','' AS N'测试值',''AS N'测试结果', ");
                        sb.Append("ttic.TestGroupID,ttic.TestID from tb_TestIndexConfig ttic where ttic.displaytype=" + displaytype.ToString() + " order by  ttic.TestGroupID,ttic.TestID");
                        dt.Merge(trm.ExecuteSqlTable(sb.ToString()));
                        dt.Columns.Remove(dt.Columns["TestGroupID"]);
                        dt.Columns.Remove(dt.Columns["TestID"]);
                        break;
                    }
                case 2:
                    {
                        StringBuilder sb = new StringBuilder();
                        sb.Append("select distinct TestGroup as N'分类',SUBSTRING(TestName, 1, case  when charindex('#',TestName )=0 then len(TestName) ");
                        sb.Append(" else charindex('#',TestName )-1 end ) AS N'测试项目',remark AS N'指标要求','' AS N'测试值',''AS N'测试结果', ");
                        sb.Append("ttic.TestGroupID,ttic.TestID from tb_TestIndexConfig ttic where ttic.displaytype=" + displaytype.ToString() + "  order by  ttic.TestGroupID,ttic.TestID");
                        dt.Merge(trm.ExecuteSqlTable(sb.ToString()));
                        dt.Columns.Remove(dt.Columns["TestGroupID"]);
                        dt.Columns.Remove(dt.Columns["TestID"]);
                        break;
                    }
                case 4:
                    {
                        StringBuilder sb = new StringBuilder();
                        sb.Append("select distinct TestGroup as N'分类',SUBSTRING(TestName, 1, case  when charindex('#',TestName )=0 then len(TestName) ");
                        sb.Append(" else charindex('#',TestName )-1 end ) AS N'测试项目',''AS N'测试结果', ");
                        sb.Append("ttic.TestGroupID,ttic.TestID from tb_TestIndexConfig ttic where ttic.displaytype=" + displaytype.ToString() + "  order by  ttic.TestGroupID,ttic.TestID");
                        dt.Merge(trm.ExecuteSqlTable(sb.ToString()));
                        dt.Columns.Remove(dt.Columns["TestGroupID"]);
                        dt.Columns.Remove(dt.Columns["TestID"]);
                        break;
                    }
                default:
                    break;
            }
            trm.CloseDataBase();
            return dt;
        }

        private void Init450MQuotaGrid()
        {
            HeaderCheckBox = new CheckBox();
            HeaderCheckBox.Size = new Size(15, 15);
            dataGridView1.Controls.Add(HeaderCheckBox);

            HeaderCheckBox.MouseClick += new MouseEventHandler(HeaderCheckBox_MouseClick);
            dataGridView1.CurrentCellDirtyStateChanged += new EventHandler(dgvSelectAll_CurrentCellDirtyStateChanged);
            DataTable dt = InitGrid(1);
            dataGridView1.DataSource = dt;
            dataGridView1.Columns[0].Width = 70;
            dataGridView1.Columns[0].ReadOnly = false;
            for (int i = 1; i < dataGridView1.Columns.Count; i++)
            {
                dataGridView1.Columns[i].ReadOnly = true;
            }
            oRectangle = this.dataGridView1.GetCellDisplayRectangle(0, -1, true);
            Color color = dataGridView1.Columns[0].HeaderCell.Style.BackColor;
            Point oPoint = new Point();
            oPoint.X = oRectangle.Location.X + (oRectangle.Width - HeaderCheckBox.Width) / 2 + 1;// 3;
            oPoint.Y = oRectangle.Location.Y + (oRectangle.Height - HeaderCheckBox.Height) / 2 + 1;
            HeaderCheckBox.Location = oPoint;
            HeaderCheckBox.BackColor = color;
            HeaderCheckBox.Checked = true;
            dataGridView1.Columns[0].HeaderText = "";

            for (int i = 0; i < dataGridView1.Columns.Count; i++)
            {
                dataGridView1.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;
            }
        }

        private void dgvSelectAll_CurrentCellDirtyStateChanged(object sender, EventArgs e)
        {
            if (dataGridView1.CurrentCell is DataGridViewCheckBoxCell)
                dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
        }

        private void HeaderCheckBox_MouseClick(object sender, MouseEventArgs e)
        {
            HeaderCheckBoxClick((CheckBox)sender);
        }

        private void HeaderCheckBoxClick(CheckBox HCheckBox)
        {
            foreach (DataGridViewRow Row in dataGridView1.Rows)
            {
                ((DataGridViewCheckBoxCell)Row.Cells["全选"]).Value = HCheckBox.Checked;
            }
            dataGridView1.RefreshEdit();
        }

        private void Grid3HeaderCheckBox_MouseClick(object sender, MouseEventArgs e)
        {
            Grid3HeaderCheckBoxClick((CheckBox)sender);
        }

        private void Grid3HeaderCheckBoxClick(CheckBox CheckBox)
        {
            foreach (DataGridViewRow Row in dataGridView3.Rows)
            {
                ((DataGridViewCheckBoxCell)Row.Cells["全选"]).Value = CheckBox.Checked;
            }
            dataGridView3.RefreshEdit();
        }

        private void InitLbjGrid()
        {
            if (HeaderCheckBox2 == null)
            {
                HeaderCheckBox2 = new CheckBox();
                HeaderCheckBox2.Size = new Size(15, 15);
            }
            dataGridView3.Controls.Add(HeaderCheckBox2);

            HeaderCheckBox2.MouseClick += new MouseEventHandler(Grid3HeaderCheckBox_MouseClick);
            dataGridView3.CurrentCellDirtyStateChanged += new EventHandler(dgvSelectAll_CurrentCellDirtyStateChanged);
            DataTable dt = InitGrid(3);
            dataGridView3.DataSource = dt;
            dataGridView3.Columns[0].Width = 70;
            dataGridView3.Columns[0].ReadOnly = false;
            for (int i = 1; i < dataGridView3.Columns.Count; i++)
            {
                dataGridView3.Columns[i].ReadOnly = true;
            }
            oRectangle = this.dataGridView3.GetCellDisplayRectangle(0, -1, true);
            Color color = dataGridView3.Columns[0].HeaderCell.Style.BackColor;
            Point oPoint = new Point();
            if (oRectangle.Width == 0)
            {
                oRectangle.Width = 105;
            }
            if (oRectangle.Height == 0)
            {
                oRectangle.Height = 45;
            }
            oPoint.X = oRectangle.Location.X + (oRectangle.Width - HeaderCheckBox2.Width) / 2 + 1;// 3;
            oPoint.Y = oRectangle.Location.Y + (oRectangle.Height - HeaderCheckBox2.Height) / 2 + 1;
            HeaderCheckBox2.Location = oPoint;
            HeaderCheckBox2.BackColor = color;
            HeaderCheckBox2.Checked = true;
            HeaderCheckBox2.Refresh();
            dataGridView3.Columns[0].HeaderText = "";

            for (int i = 0; i < dataGridView3.Columns.Count; i++)
            {
                dataGridView3.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;
            }
            dataGridView3.Invalidate();
        }

        private void InitGsmGrid()
        {
            DataTable dt2 = InitGrid(2);
            dataGridView2.DataSource = dt2;
            foreach (DataGridViewColumn Col in dataGridView2.Columns)
            {
                Col.SortMode = DataGridViewColumnSortMode.NotSortable;
            }
            dataGridView2.Rows[0].Cells[0].Selected = false;
        }

        private void InitFunGrid()
        {
            DataTable dt3 = InitGrid(4);
            dataGridView4.DataSource = dt3;
            foreach (DataGridViewColumn col in dataGridView4.Columns)
            {
                col.SortMode = DataGridViewColumnSortMode.NotSortable;
            }
        }


        private void WholeDeviceTestFrm_Load(object sender, EventArgs e)
        {

            Init450MQuotaGrid();

            InitGsmGrid();

            InitFunGrid();

            InitLbjGrid();
            dataGridView2.Columns[0].ReadOnly = true;
        }

        private void WholeDeviceTestFrm_Resize(object sender, EventArgs e)
        {
            try
            {
                if (xtraTabControl1.SelectedTabPageIndex == 0)
                {
                    oRectangle = this.dataGridView1.GetCellDisplayRectangle(0, -1, true);
                    this.dataGridView1.Invalidate();
                    StartNum = 28;
                }
            }
            catch
            {

            }
        }

        private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
            DataGridView dataGridView = sender as DataGridView;
            if (dataGridView.Name == "dataGridView1" || dataGridView.Name == "dataGridView3")
            {
                if (e.RowIndex > -1 && e.ColumnIndex == 1)
                {
                    DrawCell(e, dataGridView);
                }
            }
            else
            {
                if (e.RowIndex > -1 && e.ColumnIndex == 0)
                {
                    DrawCell(e, dataGridView);
                }
            }
        }

        #region 自定义单元格合并方法
        /// <summary>
        /// 画单元格
        /// </summary>
        /// <param name="e"></param>
        private void DrawCell(DataGridViewCellPaintingEventArgs e, DataGridView dgv)
        {
            if (e.CellStyle.Alignment == DataGridViewContentAlignment.NotSet)
            {
                e.CellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            }
            Brush gridBrush = new SolidBrush(dgv.GridColor);
            SolidBrush backBrush = new SolidBrush(Color.White);
            SolidBrush backBrush2 = new SolidBrush(Color.White);//e.CellStyle.BackColor);
            SolidBrush fontBrush = new SolidBrush(Color.White);//e.CellStyle.ForeColor);
            int cellwidth;
            int cellheight;
            int offset = 0;
            //上面相同的行数
            int UpRows = 0;
            //下面相同的行数
            int DownRows = 0;
            //总行数
            int mergerColumnIndex = 0; // 要合并的列的索引号
            int count = 0;
            if (dgv.Name == "dataGridView1" || dgv.Name == "dataGridView3")
            {
                mergerColumnIndex = 1;
            }
            else
            {
                mergerColumnIndex = 0;
            }
            if (e.RowIndex != -1)
            {
                cellwidth = e.CellBounds.Width;
                cellheight = e.CellBounds.Height;
                Pen gridLinePen = new Pen(gridBrush);
                string curValue = e.Value == null ? "" : e.Value.ToString().Trim();
                string curSelected = dgv.CurrentRow.Cells[e.ColumnIndex].Value == null ? "" : dgv.CurrentRow.Cells[e.ColumnIndex].Value.ToString().Trim();
                if (!string.IsNullOrEmpty(curValue))
                {
                    #region 获取下面的行数
                    for (int i = e.RowIndex; i < dgv.Rows.Count; i++)
                    {
                        if (dgv.Rows[i].Cells[e.ColumnIndex].Value.ToString().Equals(curValue) && e.ColumnIndex == mergerColumnIndex)
                        {
                            dgv.Rows[i].Cells[e.ColumnIndex].Selected = dgv.Rows[e.RowIndex].Cells[e.ColumnIndex].Selected;
                            DownRows++;
                            if (e.RowIndex != i)
                            {
                                cellwidth = cellwidth < dgv.Rows[i].Cells[e.ColumnIndex].Size.Width ? cellwidth : dgv.Rows[i].Cells[e.ColumnIndex].Size.Width;
                                cellheight = Math.Min(cellheight, dgv.Rows[i].Cells[e.ColumnIndex].Size.Height);

                                // offset  += Math.Abs(cellheight - dgv.Rows[i].Cells[e.ColumnIndex].Size.Height);
                            }
                        }
                        else
                        {
                            break;
                        }
                    }
                    #endregion

                    #region 获取上面的行数
                    for (int i = e.RowIndex; i >= 0; i--)
                    {
                        if (dgv.Rows[i].Cells[e.ColumnIndex].Value.ToString().Equals(curValue) && e.ColumnIndex == mergerColumnIndex)
                        {
                            dgv.Rows[i].Cells[e.ColumnIndex].Selected = dgv.Rows[e.RowIndex].Cells[e.ColumnIndex].Selected;
                            UpRows++;
                            if (e.RowIndex != i)
                            {
                                cellwidth = cellwidth < dgv.Rows[i].Cells[e.ColumnIndex].Size.Width ? cellwidth : dgv.Rows[i].Cells[e.ColumnIndex].Size.Width;
                                cellheight = Math.Min(cellheight, dgv.Rows[i].Cells[e.ColumnIndex].Size.Height);
                                offset += Math.Abs(cellheight - dgv.Rows[i].Cells[e.ColumnIndex].Size.Height);
                            }
                        }
                        else
                        {
                            break;
                        }
                    }
                    #endregion
                    count = DownRows + UpRows - 1;
                    if (count < 2)
                    {
                        return;
                    }

                }
                if (dgv.Rows[e.RowIndex].Selected)
                {
                    backBrush.Color = Color.White;// e.CellStyle.SelectionForeColor;
                    fontBrush.Color = Color.White;// e.CellStyle.SelectionForeColor;
                }
                //以背景色填充

                if (dgv.Columns[e.ColumnIndex].Name == "测试值" || dgv.Columns[e.ColumnIndex].Name == "测试结果" || dgv.Columns[e.ColumnIndex].Name == "试验结果")
                    e.Graphics.FillRectangle(backBrush2, e.CellBounds);
                else
                    e.Graphics.FillRectangle(backBrush, e.CellBounds);
                if (dgv.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString() == "")
                    e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);
                //画字符串
                PaintingFont(e, cellwidth, UpRows, DownRows, count, cellheight, offset);
                // dgv.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = "";
                if (DownRows == 1)
                {
                    e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);
                    count = 0;
                }
                //// 画右边线
                //if (dgv.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString() != "")
                e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom);

                e.Handled = true;
                dgv.DefaultCellStyle.WrapMode = DataGridViewTriState.True;
            }
        }
        /// <summary>
        /// 画字符串
        /// </summary>
        /// <param name="e"></param>
        /// <param name="cellwidth"></param>
        /// <param name="UpRows"></param>
        /// <param name="DownRows"></param>
        /// <param name="count"></param>
        private void PaintingFont(System.Windows.Forms.DataGridViewCellPaintingEventArgs e, int cellwidth, int UpRows, int DownRows, int count, int cellheight, int offset)
        {
            SolidBrush fontBrush = new SolidBrush(e.CellStyle.ForeColor);
            int fontheight = (int)e.Graphics.MeasureString(e.Value.ToString(), e.CellStyle.Font).Height;
            int fontwidth = (int)e.Graphics.MeasureString(e.Value.ToString(), e.CellStyle.Font).Width;
            // int cellheight = cellheight;
            if (e.CellStyle.Alignment == DataGridViewContentAlignment.BottomCenter)
            {
                System.Drawing.Rectangle rec = new System.Drawing.Rectangle(e.CellBounds.X + (cellwidth - fontwidth) / 2, e.CellBounds.Y + cellheight * DownRows - fontheight, e.CellBounds.Width, e.CellBounds.Height);
                e.Graphics.DrawString((String)e.Value, e.CellStyle.Font, fontBrush, rec);
            }
            else if (e.CellStyle.Alignment == DataGridViewContentAlignment.BottomLeft)
            {
                System.Drawing.Rectangle rec = new System.Drawing.Rectangle(e.CellBounds.X, e.CellBounds.Y + cellheight * DownRows - fontheight, e.CellBounds.Width, e.CellBounds.Height);
                e.Graphics.DrawString((String)e.Value, e.CellStyle.Font, fontBrush, rec);
            }
            else if (e.CellStyle.Alignment == DataGridViewContentAlignment.BottomRight)
            {
                System.Drawing.Rectangle rec = new System.Drawing.Rectangle(e.CellBounds.X + cellwidth - fontwidth, e.CellBounds.Y + cellheight * DownRows - fontheight, e.CellBounds.Width, e.CellBounds.Height);
                e.Graphics.DrawString((String)e.Value, e.CellStyle.Font, fontBrush, rec);
            }
            else if (e.CellStyle.Alignment == DataGridViewContentAlignment.MiddleCenter)
            {
                System.Drawing.Rectangle rec = new System.Drawing.Rectangle(e.CellBounds.X + (cellwidth - fontwidth) / 2, e.CellBounds.Y - cellheight * (UpRows - 1) + (cellheight * count - fontheight) / 2, e.CellBounds.Width, e.CellBounds.Height);
                e.Graphics.DrawString((String)e.Value, e.CellStyle.Font, fontBrush, rec);
            }
            else if (e.CellStyle.Alignment == DataGridViewContentAlignment.MiddleLeft)
            {
                System.Drawing.Rectangle rec = new System.Drawing.Rectangle(e.CellBounds.X, e.CellBounds.Y - cellheight * (UpRows - 1) + (cellheight * count - fontheight) / 2 - offset, e.CellBounds.Width, e.CellBounds.Height);//
                e.Graphics.DrawString((String)e.Value, e.CellStyle.Font, fontBrush, rec);
            }
            else if (e.CellStyle.Alignment == DataGridViewContentAlignment.MiddleRight)
            {
                System.Drawing.Rectangle rec = new System.Drawing.Rectangle(e.CellBounds.X + cellwidth - fontwidth, e.CellBounds.Y - cellheight * (UpRows - 1) + (cellheight * count - fontheight) / 2, e.CellBounds.Width, e.CellBounds.Height);
                e.Graphics.DrawString((String)e.Value, e.CellStyle.Font, fontBrush, rec);
            }
            else if (e.CellStyle.Alignment == DataGridViewContentAlignment.TopCenter)
            {
                System.Drawing.Rectangle rec = new System.Drawing.Rectangle(e.CellBounds.X + (cellwidth - fontwidth) / 2, e.CellBounds.Y - cellheight * (UpRows - 1), e.CellBounds.Width, e.CellBounds.Height);
                e.Graphics.DrawString((String)e.Value, e.CellStyle.Font, fontBrush, rec);
            }
            else if (e.CellStyle.Alignment == DataGridViewContentAlignment.TopLeft)
            {
                System.Drawing.Rectangle rec = new System.Drawing.Rectangle(e.CellBounds.X, e.CellBounds.Y - cellheight * (UpRows - 1), e.CellBounds.Width, e.CellBounds.Height);
                e.Graphics.DrawString((String)e.Value, e.CellStyle.Font, fontBrush, rec);
            }
            else if (e.CellStyle.Alignment == DataGridViewContentAlignment.TopRight)
            {
                System.Drawing.Rectangle rec = new System.Drawing.Rectangle(e.CellBounds.X + cellwidth - fontwidth, e.CellBounds.Y - cellheight * (UpRows - 1), e.CellBounds.Width, e.CellBounds.Height);
                e.Graphics.DrawString((String)e.Value, e.CellStyle.Font, fontBrush, rec);
            }
            else
            {
                System.Drawing.Rectangle rec = new System.Drawing.Rectangle(e.CellBounds.X + (cellwidth - fontwidth) / 2, e.CellBounds.Y - cellheight * (UpRows - 1) + (cellheight * count - fontheight) / 2, e.CellBounds.Width, e.CellBounds.Height);
                e.Graphics.DrawString((String)e.Value, e.CellStyle.Font, fontBrush, rec);
            }
        }
        #endregion

        private void xtraTabControl1_SelectedPageChanged(object sender, DevExpress.XtraTab.TabPageChangedEventArgs e)
        {
            selectedPageName = e.Page.Text;
            if (e.Page.Text == "GSM-R电性能指标测试")
            {
                dataGridView2.ClearSelection();
            }
            if (e.Page.Text == "800M电性能测试")
            {
               // InitLbjGrid();
            }
        }

        private void WholeDeviceTestFrm_FormClosing(object sender, FormClosingEventArgs e)
        {

            DisposeObject();
        }

        private void Clear450MSource()
        {
            if (TranTest != null)
            {
                TranTest.ClearSource();
            }
            if (CoRecTest != null)
            {
                CoRecTest.ClearSource();
            }
            if (PiRecTest != null)
            {
                PiRecTest.ClearSource();
            }
            if (SigTest != null)
            {
                SigTest.ClearSource();
            }

        }

        public void DisposeObject()
        {
            if (LbjFunTrial != null)
            {
                LbjFunTrial.ClearSoure();
            }
            if (TrainEndFunTrial != null)
            {
                TrainEndFunTrial.ClearSource();
            }
            if (GsmDigTest != null)
            {
                GsmDigTest.ClearSource();
            }
            if (GsmVoTest != null)
            {
                GsmVoTest.ClearSource();
            }
            if (TrainApproach != null)
            {
                TrainApproach.ClearSource();
            }
            if (_450MDispatchTrial != null)
            {
                _450MDispatchTrial.ClearSource();
            }
            if (LbjTranTest != null)
            {
                LbjTranTest.ClearSoure();
            }
            if (LbjRecTest != null)
            {
                LbjRecTest.ClearSoure();
            }
            Clear450MSource();
        }

        private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            DataGridView dataGridView = sender as DataGridView;
            if (e.RowIndex != -1 && e.ColumnIndex == 0)
            {
                if (e.RowIndex == 18 || e.RowIndex == 19)
                {
                    if (e.RowIndex == 18)
                    {
                        dataGridView.Rows[19].Cells[0].Value = ((bool)dataGridView.Rows[18].Cells[0].Value);

                    }
                    else
                    {
                        dataGridView.Rows[18].Cells[0].Value = ((bool)dataGridView.Rows[19].Cells[0].Value);
                    }
                }
                if (e.RowIndex == 0 + StartNum || e.RowIndex == 8 + StartNum)
                {
                    if (e.RowIndex == 0 + StartNum)
                    {
                        dataGridView.Rows[8 + StartNum].Cells[0].Value = ((bool)dataGridView.Rows[0 + StartNum].Cells[0].Value);

                    }
                    else
                    {
                        dataGridView.Rows[0 + StartNum].Cells[0].Value = ((bool)dataGridView.Rows[8 + StartNum].Cells[0].Value);
                    }
                }
                if (e.RowIndex == 1 + StartNum || e.RowIndex == 9 + StartNum)
                {
                    if (e.RowIndex == 1 + StartNum)
                    {
                        dataGridView.Rows[9 + StartNum].Cells[0].Value = ((bool)dataGridView.Rows[1 + StartNum].Cells[0].Value);

                    }
                    else
                    {
                        dataGridView.Rows[1 + StartNum].Cells[0].Value = ((bool)dataGridView.Rows[9 + StartNum].Cells[0].Value);
                    }
                }
                if (e.RowIndex == 2 + StartNum || e.RowIndex == 10 + StartNum)
                {
                    if (e.RowIndex == 2 + StartNum)
                    {
                        dataGridView.Rows[10 + StartNum].Cells[0].Value = ((bool)dataGridView.Rows[2 + StartNum].Cells[0].Value);

                    }
                    else
                    {
                        dataGridView.Rows[2 + StartNum].Cells[0].Value = ((bool)dataGridView.Rows[10 + StartNum].Cells[0].Value);
                    }
                }
                if (e.RowIndex == 3 + StartNum || e.RowIndex == 11 + StartNum)
                {
                    if (e.RowIndex == 3 + StartNum)
                    {
                        dataGridView.Rows[11 + StartNum].Cells[0].Value = ((bool)dataGridView.Rows[3 + StartNum].Cells[0].Value);

                    }
                    else
                    {
                        dataGridView.Rows[3 + StartNum].Cells[0].Value = ((bool)dataGridView.Rows[11 + StartNum].Cells[0].Value);
                    }
                }
                if (e.RowIndex == 4 + StartNum || e.RowIndex == 12 + StartNum)
                {
                    if (e.RowIndex == 4 + StartNum)
                    {
                        dataGridView.Rows[12 + StartNum].Cells[0].Value = ((bool)dataGridView.Rows[4 + StartNum].Cells[0].Value);

                    }
                    else
                    {
                        dataGridView.Rows[4 + StartNum].Cells[0].Value = ((bool)dataGridView.Rows[12 + StartNum].Cells[0].Value);
                    }
                }
                if (e.RowIndex == 5 + StartNum || e.RowIndex == 13 + StartNum)
                {
                    if (e.RowIndex == 5 + StartNum)
                    {
                        dataGridView.Rows[13 + StartNum].Cells[0].Value = ((bool)dataGridView.Rows[5 + StartNum].Cells[0].Value);

                    }
                    else
                    {
                        dataGridView.Rows[5 + StartNum].Cells[0].Value = ((bool)dataGridView.Rows[13 + StartNum].Cells[0].Value);
                    }
                }
                if (e.RowIndex == 6 + StartNum || e.RowIndex == 14 + StartNum)
                {
                    if (e.RowIndex == 6 + StartNum)
                    {
                        dataGridView.Rows[14 + StartNum].Cells[0].Value = ((bool)dataGridView.Rows[6 + StartNum].Cells[0].Value);

                    }
                    else
                    {
                        dataGridView.Rows[6 + StartNum].Cells[0].Value = ((bool)dataGridView.Rows[14 + StartNum].Cells[0].Value);
                    }
                }
                if (e.RowIndex == 7 + StartNum || e.RowIndex == 15 + StartNum)
                {
                    if (e.RowIndex == 7 + StartNum)
                    {
                        dataGridView.Rows[15 + StartNum].Cells[0].Value = ((bool)dataGridView.Rows[7 + StartNum].Cells[0].Value);

                    }
                    else
                    {
                        dataGridView.Rows[7 + StartNum].Cells[0].Value = ((bool)dataGridView.Rows[15 + StartNum].Cells[0].Value);
                    }
                }
            }
        }
    }
}
